#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    tcpServer =new QTcpServer(this);
    tcpSocket =new QTcpSocket(this);

    tcpServer->listen(QHostAddress::Any,23333);

    camera=new QCamera;
    viewfinder=new QCameraViewfinder(this);
    imageCapture=new QCameraImageCapture(camera);
    camera->setViewfinder(viewfinder);

    //connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ShowTheCapture()));
    //connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(SavePicture()));
    connect(imageCapture,SIGNAL(imageCaptured(int,QImage)),this,SLOT(displayImage(int,QImage)));
    imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    camera->setCaptureMode(QCamera::CaptureStillImage);
    camera->setViewfinder(viewfinder);
    camera->start();

    connect(&timer,SIGNAL(timeout()),this,SLOT(Save()));
    //timer.start(200);
    connect(&timer2,SIGNAL(timeout()),this,SLOT(Seed()));
    //timer2.start(200);

    connect(tcpServer,&QTcpServer::newConnection,[=]{
      tcpSocket=tcpServer->nextPendingConnection();
      QString ip=tcpSocket->peerAddress().toString();
      qint16 port=tcpSocket->peerPort();
      QString temp=QString("[%1,%2]:连接成功").arg(ip).arg(port);
      qDebug()<<temp;

      connect(tcpSocket,&QTcpSocket::readyRead,[=]{
        QString s=tcpSocket->readAll().toStdString().data();
        qDebug()<<s;
        if(s=="py" && state == 0){
            state = 1;
            Save();
        }
        else if(state == 0){
            state = 2;
        }
        if(s=="ok"){
            Save();
        }
      });
    });

    collimation->setText("<span style='color:#ff0000'>★</span>");
    collimation->resize(20,20);
    collimation->raise();
    collimation->move(310,230);
    //collimation->move(640,480);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::ShowTheCapture()
{
    imageCapture->capture();
}

void Widget::SavePicture()
{
    /*const QPixmap *pixmap=ui->label->pixmap();
    if(pixmap)
    {
        pixmap->save("E:\\hallo.jpg");
        qDebug()<<"保存"<<endl;
    }//*/

}

void Widget::displayImage(int, QImage image)
{
    //image=image.convertToFormat(QImage::Format_RGB888);
    label->setPixmap(QPixmap::fromImage(image));
    /*QImage::Format format=image.format();
    qDebug()<<(int)format;//*/


}

void Widget::Save()
{
    imageCapture->capture();
    const QPixmap *pix=label->pixmap();
    if(pix)
    {
        pix->save("F:\\YOLO\\res\\img.jpg");
    }//*/
    //qDebug()<<"1";
    if(state == 1){
        tcpSocket->write("save");
    }
    else if(state == 2){
        tcpSocket2->write("save");
    }
    //qDebug()<<"2";
}

void Widget::Seed()
{
    QFile file("F:\\YOLO\\res\\x.txt");
    file.open(QIODevice::ReadOnly);
    QString s=file.readAll();
    file.close();
    qDebug()<<"s="<<s<<endl;
    int x1,y1,x2,y2;
    sscanf(s.toStdString().data(),"%d %d %d %d",&x1,&y1,&x2,&y2);
    qDebug()<<"x1="<<x1;
    qDebug()<<"y1="<<y1;
    qDebug()<<"x2="<<x2;
    qDebug()<<"x1="<<y2;
    int x=(x1+x2)/2;
    int y=(y1+y2)/2;

    if(x<dx){
        dx-=5;
        collimation->move(collimation->x()-5,collimation->y());
        tcpSocket->write("a");
    }else if(x>dx){
        dx+=5;
        collimation->move(collimation->x()+5,collimation->y());
        tcpSocket->write("d");
    }
    if(y<dy){
        dy-=5;
        collimation->move(collimation->x(),collimation->y()-5);
        tcpSocket->write("w");
    }else if(y>dy){
        dy+=5;
        collimation->move(collimation->x(),collimation->y()+5);
        tcpSocket->write("s");
    }



}










